home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 34.zip / BS1 part 34 / FredFish PD 307.adf / Samp / Exam.c < prev    next >
C/C++ Source or Header  |  1990-01-13  |  8KB  |  285 lines

  1. /***************************************************************************
  2.     Set your editor's TAB width to 3.
  3.  
  4.     This is an example C application using the dissidents' SAMP library.
  5. It uses the dissidents' requester.library to allow the user to choose a
  6. SAMP file to load. It then loads the SAMP file, and plays back all of the midi
  7. notes (0 to 127) of the PlayMap using the audio device. This example is in the
  8. public domain, and may be used by any other program.
  9.  
  10.     Written by Jim Fiore and Jeff Glatt, dissidents
  11.  
  12. ****************************************************************************/
  13.  
  14. #include "exec/types.h"
  15. #include "exec/memory.h"
  16. #include "hardware/custom.h"
  17. #include "hardware/dmabits.h"
  18. #include "libraries/dos.h"
  19. #include "libraries/dosextens.h"
  20. #include "devices/audio.h"
  21.  
  22. #include "functions.h"
  23. #include "exec/tasks.h"
  24. #include "intuition/intuition.h"
  25. #include "intuition/intuitionbase.h"
  26.  
  27. #include "graphics/gfx.h"
  28. #include "graphics/gfxbase.h"
  29. #include "graphics/rastport.h"
  30. #include "graphics/gfxmacros.h"
  31. #include "graphics/view.h"
  32. #include "graphics/text.h"
  33.  
  34. /*=============== Include files for dissidents' custom libraries ===========*/
  35. #include "FileIO.h"
  36. #include "SAMP.h"
  37.  
  38. /*=================== For audio.device playback of wave ==================*/
  39. struct IOAudio ioaudio = {0}, ioaudio2 = {0};
  40. struct MsgPort *audPort = 0L;
  41. UBYTE  audio_gub = 0;
  42. UBYTE  anychan[4] = {1,2,4,8};
  43.  
  44. /*================== For dissidents requester.library =================*/
  45. struct RequesterBase          *RequesterBase = 0L;
  46. struct FileIO                 *fileio_sup = 0L;
  47. UBYTE  fileio_filename[260];
  48.  
  49. /*==================== For dissidents samp.library =====================*/
  50. extern LONG SAMPError;            /* This comes from SAMPInterface.asm */
  51. struct SAMPBase               *SAMPBase = 0L;
  52. struct SAMPInfo                    *libSAMPinfo = 0L;
  53. #define playCHANS 4
  54. UBYTE  play_map[128*playCHANS];  /* let NumOfChans = 4 */
  55. struct TransposeNode          *T_List = 0L;
  56.  
  57. /* Declare 32 SampleHeader64 structs for loading up to 32 waves in a SAMP */
  58. #define  MAXSAMPLES 32
  59. #define  NAMESIZE   16
  60.  
  61. struct SampleHeader64 samp_head[MAXSAMPLES];
  62.  
  63. UBYTE  nameBuffers[NAMESIZE*MAXSAMPLES];  /* for the wave names */
  64.  
  65.  
  66. /*====================== The C Application begins here ==================*/
  67.  
  68.  
  69. VOID open_all(), exit_all();
  70.  
  71. main()
  72. {
  73.     ULONG    totalWaves, err, p;
  74.     LONG  transposeAmount;
  75.     USHORT waveNum, i;
  76.     UBYTE *address, b;
  77.    struct TransposeNode *transL, *transL2;
  78.  
  79.     /* Open libs, initialize audio dev and structures, get FileIO struct */
  80.     open_all();
  81.  
  82.     /* choose the SAMP file to load via the requester.library */
  83.     address = DoFileIOWindow(fileio_sup, 0L);
  84.     if( address == fileio_filename )
  85.     {
  86.  
  87.         /* Open the file and determine if it is a SAMP */
  88.         if( !(libSAMPinfo = OpenSampRead( fileio_filename, 0L )) )
  89.         {
  90.             address = SAMPErrorMsg( SAMPError );
  91.             puts( address );
  92.             exit_all();
  93.         }
  94.         libSAMPinfo->MaxChars = NAMESIZE;
  95.  
  96.         /* Load up to 32 waves starting with the first wave in the SAMP (0)
  97.             We'll load these into our samp_head and nameBuffers starting with
  98.             the first member of each array. */
  99.         if( !(totalWaves = ReadWaves( 0L, 32L , &nameBuffers[0], &samp_head[0] )) )
  100.         {
  101.             /* We didn't load ANY waves! Must be an empty SAMP. */
  102.             CloseSamp();
  103.             address = SAMPErrorMsg( SAMPError );
  104.             puts( address );
  105.             exit_all();
  106.         }
  107.  
  108.         /* This zeros out our playMap and transfers the loaded playMap to our
  109.             passed playMap. It adjusts for difference in numOfChans between our
  110.             program's playMap (4 chans) and the loaded playMap (0 to 4 chans).
  111.             Also, it ensures that only those wave numbers that we loaded appear
  112.             in our play_map. We pass an offset of 0 because we loaded from the
  113.             first SampleHeader64 in our samp_head array. */
  114.         LoadPlaymap(totalWaves, 4L, 0L, 0L, &play_map[0]);
  115.  
  116.         for(i=0; i<totalWaves; ++i)
  117.        {
  118.             /* The SAMP library set the TransposeTable fields of each loaded
  119.                 SampleHeader64 struct to that wave's sampleRate. Now we use this
  120.                 function to get all of the needed TransposeTables. This function
  121.                 links any new TranposeTable into our T_List and returns the
  122.                 address of that TransposeTable's ORIGINAL_PITCH. */
  123.             samp_head[i].TransTable = MakeTransTable( samp_head[i].TransTable, 6L, 6L, &T_List );
  124.         }
  125.  
  126.         CloseSamp();
  127.  
  128.  
  129. /* playback the wave using audio.device. Do multi playMode (i.e. only use
  130.    the 1st byte for each midi note in the playMap). */
  131.         ioaudio.ioa_Volume = 64;    /* Play everything with MAX volume and forget velocity table */
  132.         ioaudio2.ioa_Volume = 64;
  133.         ioaudio.ioa_Request.io_Command = CMD_WRITE;
  134.         ioaudio2.ioa_Request.io_Command = CMD_WRITE;
  135.  
  136.         for( i=0; i<128; ++i ) 
  137.         {                       /* play all 128 midi notes */
  138.             waveNum = i*4;
  139.             if( b = play_map[waveNum] )
  140.            {
  141.                 --b;
  142.                 waveNum = (USHORT)b;    /* to adjust wave number from 0 instead of 1 */
  143.  
  144.                 puts("\nPlaying...");
  145.                 puts(&nameBuffers[b*NAMESIZE]);
  146.  
  147.                 ioaudio.ioa_Data = (UBYTE *)samp_head[waveNum].OneShotAddr;
  148.                 ioaudio.ioa_Length = samp_head[waveNum].OneShotEnd - samp_head[waveNum].OneShotAddr;
  149.                 ioaudio.ioa_Request.io_Flags = ADIOF_PERVOL;
  150.                 ioaudio.ioa_Cycles = 1;
  151.                 if( samp_head[waveNum].TransTable )
  152.                {
  153.                     transposeAmount = (LONG)i - (LONG)(samp_head[waveNum].RootNote);
  154.                     ioaudio.ioa_Period = *(samp_head[waveNum].TransTable + transposeAmount); /* transposeAmount automatically x2 */
  155.                 }
  156.                 else
  157.                     /* Actually, we could've saved the original sample rate so that
  158.                         we could play it back at its original pitch if the loaded
  159.                         MHDR numOfChans = 0, or we can't make a transpose table. */
  160.                     break;
  161.  
  162.                 ioaudio2.ioa_Data = (UBYTE *)samp_head[waveNum].OneShotEnd;
  163.                 ioaudio2.ioa_Request.io_Flags = 0;
  164.                 ioaudio2.ioa_Cycles = 1;        /* only play the loop once. */
  165.                 ioaudio2.ioa_Period = ioaudio.ioa_Period;
  166.                 BeginIO( &ioaudio );
  167.  
  168.                 if( ioaudio2.ioa_Length = samp_head[waveNum].LoopLength * 2 ) 
  169.                     BeginIO( &ioaudio2 );
  170.                 err = WaitIO( &ioaudio );
  171.                 GetMsg( audPort );
  172.                 if(ioaudio2.ioa_Length) 
  173.                 {
  174.                     err = WaitIO( &ioaudio2 );
  175.                     GetMsg( audPort );
  176.                 }
  177.             }
  178.         }
  179.  
  180. /* Free the sample memory (for "total" # of waves) */
  181.         for(p=0; p<totalWaves; ++p)
  182.        {
  183.             FreeMem(samp_head[p].OneShotAddr,samp_head[p].WaveSize);
  184.         }
  185.  
  186.  
  187. /* Free the TransposeTables */
  188.         if( (transL = T_List) != 0 )
  189.         {
  190.             while( transL )
  191.             {
  192.                 transL2 = transL->Next;
  193.                 FreeMem( transL, transL->TSize );
  194.                 transL = transL2;
  195.             }
  196.         }
  197.  
  198. /* Now close down and get some food */
  199.         exit_all();
  200.     }
  201.  
  202. }   /********** end of main() ********/
  203.  
  204.  
  205.  
  206. /*---------- Opens audio dev, audio port, SAMP and FileIO libs ---------*/
  207.  
  208. VOID open_all()
  209. {
  210.     ULONG err,x,total;
  211.     
  212.  
  213. /* Open the requester library and allocate/setup a FileIO structure */
  214.     if( !(RequesterBase=(struct RequesterBase *)OpenLibrary("requester.library", 1L)) )
  215.     {
  216.         puts("need requester.library, Not ARP\n");
  217.         exit_all();
  218.     }
  219.  
  220.     if( !(fileio_sup = GetFileIO()) )
  221.     {
  222.         puts("Can't get FileIO support\n");
  223.         exit_all();
  224.     }
  225.  
  226.     fileio_sup->X = 6;
  227.     fileio_sup->Y = 11;
  228.     fileio_sup->DrawMode = JAM2;
  229.     fileio_sup->PenA = 0;
  230.     fileio_sup->PenB = 1;
  231.     fileio_sup->Buffer = (UBYTE *)fileio_filename;
  232.  
  233. /* Open the SAMP reader/writer library */
  234.     if( !(SAMPBase=(struct SAMPBase *)OpenLibrary("samp.library", 0L)) )
  235.     {
  236.         puts("need samp.library you knuckle-head\n");
  237.         exit_all();
  238.     }
  239.  
  240. /* Open audio.device and allocate a channel */
  241.  
  242.     if( !(audPort = CreatePort( "SAMP_Port", 0 )) )
  243.     {
  244.         puts("no audio port");
  245.         exit_all();
  246.     }
  247.  
  248.     ioaudio.ioa_Request.io_Message.mn_ReplyPort = audPort;
  249.  
  250.     ioaudio.ioa_Request.io_Command = ADCMD_ALLOCATE;
  251.     ioaudio.ioa_Data = (UBYTE *)anychan;
  252.     ioaudio.ioa_Length = 4;
  253.     ioaudio.ioa_Request.io_Flags = ADIOF_NOWAIT | IOF_QUICK;
  254.     if( OpenDevice("audio.device", 0, &ioaudio, 0 ) )
  255.     {
  256.         puts("audio device open error\n");
  257.         exit_all();
  258.     }
  259.  
  260.     if( !(ioaudio.ioa_Request.io_Flags & IOF_QUICK ) )
  261.         GetMsg( audPort );
  262.  
  263.     audio_gub = 1;
  264.     
  265.     ioaudio2 = ioaudio;
  266.     /* initialize the AudioIO for the looping portion */
  267.  
  268. }
  269.  
  270.  
  271.  
  272.  
  273. /*-----------closes audio dev, audio port, FileIO and SAMP libs---------*/
  274.  
  275. VOID exit_all()
  276. {
  277.     if( audio_gub )     CloseDevice( &ioaudio );
  278.     if( audPort )       DeletePort( audPort );
  279.     if( fileio_sup )    ReleaseFileIO( fileio_sup );
  280.     if( RequesterBase ) CloseLibrary( RequesterBase );
  281.     if( SAMPBase )      CloseLibrary( SAMPBase );
  282.     exit( FALSE );
  283.  
  284. }
  285.